<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : API Model
 * @description : Database functions related to api apps
 * 
 */
class Api_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  // Get an app by token
  function get_app_by_token($app_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($app_id)) {
      $query_cond = array();
      $query_cond['app_token'] = $app_id;
      $query_cond['app_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = app_user_id');
      $query_exec = $this->db->get_where('api_apps', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Create a new app
  function set_app($app_name = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($app_name) && !empty($user_id) && is_numeric($user_id)) {
      $query_insr = array(
          'app_token' => md5(microtime()),
          'app_name' => $app_name,
          'app_user_id' => $user_id,
          'app_create_date' => date("Y-m-d H:i:s"),
          'app_status' => 1
      );
      $query_exec = $this->db->insert('api_apps', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result']['app_id'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Update an apps name
  function update_app($app_id = NULL, $app_name = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($app_name) && !empty($app_id) && is_numeric($app_id)) {
      $query_upda = array('app_name' => $app_name);
      $query_cond = array('app_id' => $app_id);
      $query_exec = $this->db->update('api_apps', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Delete an app by app id
  function delete_app($app_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($app_id) && is_numeric($app_id)) {
      $query_upda = array('app_status' => 0);
      $query_cond = array('app_id' => $app_id);
      $query_exec = $this->db->update('api_apps', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get apps list
  function get_api_apps($limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $query_cond['app_status'] = 1;
    $query_cond['user_status'] = 1;
    $this->db->join('users', 'user_id = app_user_id');
    $this->db->order_by('app_id', 'desc');
    $query_exec = $this->db->get_where('api_apps', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  // Add an authorize app to user
  function authorize_app($app_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($app_id) && is_numeric($app_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_insr = array(
          'authorize_app_id' => $app_id,
          'authorize_user_id' => $user_id
      );
      $query_sql = $this->db->insert_string('api_apps_authorize', $query_insr);
      $query_sql = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $query_sql);
      $query_exec = $this->db->query($query_sql);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = 1;
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Remove an authorized app from user
  function un_authorize_app($app_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($app_id) && is_numeric($app_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_cond = array(
          'authorize_app_id' => $app_id,
          'authorize_user_id' => $user_id
      );
      $query_exec = $this->db->delete('api_apps_authorize', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = 1;
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_app_authorization($app_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($app_id) && is_numeric($app_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_cond = array(
          'authorize_app_id' => $app_id,
          'authorize_user_id' => $user_id,
          'user_status' => 1,
          'app_status' => 1
      );
      $this->db->join('api_apps', 'app_id = authorize_app_id');
      $this->db->join('users', 'user_id = authorize_user_id');
      $query_exec = $this->db->get_where('api_apps_authorize', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get list of apps of a user
  function get_my_apps($user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array(
          'authorize_user_id' => $user_id,
          'app_status' => 1
      );
      $this->db->join('api_apps', 'app_id = authorize_app_id');
      $this->db->join('users', 'user_id = authorize_user_id');
      $query_exec = $this->db->get_where('api_apps_authorize', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

}

?>